package com.hy.frame.recycler;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class LinearItemDecoration extends BaseItemDecoration {
    public LinearItemDecoration(int divider) {
        super(divider);
    }

    public LinearItemDecoration(int divider, int color) {
        super(divider, color);
    }

    public LinearItemDecoration(int divider, @Nullable Drawable drawable) {
        super(divider, drawable);
    }

    @Override
    protected void drawDivider(@NonNull Canvas canvas, @NonNull RecyclerView parent) {
        RecyclerView.Adapter<?> adapter = parent.getAdapter();
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (adapter == null || manager == null) return;
        if (getDivider() == 0) {
            return;
        }
        int orientation = RecyclerView.VERTICAL;
        if (manager instanceof LinearLayoutManager) {
            orientation = ((LinearLayoutManager) manager).getOrientation();
        }
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int viewAdapterPosition = params.getViewAdapterPosition();
            int viewType = adapter.getItemViewType(viewAdapterPosition);
            if (!containsType(viewType)) continue; //包含的Item类型时才绘制
            int count = adapter.getItemCount();
            if (viewAdapterPosition == count - 1 && !isEndDivider()) {//最有一条
                continue;
            }
            if (orientation == RecyclerView.VERTICAL) {//分割线不会绘制顶部
                //顶部分割线
                if (isStartDivider() && viewAdapterPosition == 0) {
                    drawRectDivider(canvas, child.getLeft(), child.getTop() - getDivider(), child.getRight(), child.getTop());
                }
                //底部分割线
                if ((viewAdapterPosition + 1) == count) {
                    if (!isEndDivider()) {
                        continue;
                    }
                }
                drawRectDivider(canvas, child.getLeft(), child.getBottom(), child.getRight(), child.getBottom() + getDivider());
            } else {//分割线不会绘制左边
                //左侧分割线
                if (isStartDivider() && viewAdapterPosition == 0) {
                    drawRectDivider(canvas, child.getLeft() - getDivider(), child.getTop(), child.getLeft(), child.getBottom());
                }
                //右侧分割线
                if ((viewAdapterPosition + 1) == count) {
                    if (!isEndDivider()) {
                        continue;
                    }
                }
                drawRectDivider(canvas, child.getRight(), child.getTop(), child.getRight() + getDivider(), child.getBottom());
            }
        }
    }

    @Override
    protected void configureItemOutRect(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent) {
        RecyclerView.Adapter<?> adapter = parent.getAdapter();
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (adapter == null || manager == null) {
            outRect.set(0, 0, 0, 0);
            return;
        }
        int orientation = RecyclerView.VERTICAL;
        if (manager instanceof LinearLayoutManager) {
            orientation = ((LinearLayoutManager) manager).getOrientation();
        }
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
        int viewAdapterPosition = params.getViewAdapterPosition();
        int viewType = adapter.getItemViewType(viewAdapterPosition);
        if (!containsType(viewType)) { //包含的Item类型时才绘制
            outRect.set(0, 0, 0, 0);
            return;
        }
        int count = adapter.getItemCount();
        int left = 0;
        int top = 0;
        int right;
        int bottom = 0;
        if (orientation == RecyclerView.VERTICAL) {
            left = getPadding().left;
            if (viewAdapterPosition == 0) {//第一条
                top = getPadding().top;
                if (isStartDivider()) {//第一条顶部是否需要添加分割线
                    top += getDivider();
                }
            }
            right = getPadding().right;
            if (viewAdapterPosition == count - 1) {//最后一条
                bottom = getPadding().bottom;
                if (isEndDivider()) {//最后一条底部是否需要添加分割线
                    bottom += getDivider();
                }
            } else {
                bottom = getDivider();
            }
        } else {
            if (viewAdapterPosition == 0) {//第一条
                left = getPadding().left;
                if (isStartDivider()) {//第一条顶部是否需要添加分割线
                    left += getDivider();
                }
            }
            top = getPadding().top;
            if (viewAdapterPosition == count - 1) {//最后一条
                right = getPadding().right;
                if (isEndDivider()) {//最后一条底部是否需要添加分割线
                    right += getDivider();
                }
            } else {
                right = getDivider();
            }
            bottom = getPadding().bottom;
        }
        outRect.set(left, top, right, bottom);
    }
}
